import * as types from '../constants/ActionTypes';

function updateProductAttr(products, pid, key, value){
    if(products && pid && key){
        for(let i = 0, len = products.length; i<len; i++){
            let product = products[i];
            if (product.gsid == pid) {
                product[key] = value;
                //如果是产品关注/取消关注，改变产品对应的关注人数
                //这样写其实是很挫的。
                if (key === "status") {
                    if (value === "1") {
                        product.viewcount = (parseInt(product.viewcount)+1);
                    }else{
                        product.viewcount = (parseInt(product.viewcount)-1);
                    }
                }
            }
        }
    }
}

function updateProductHistory(products, pid, value){
    if (products && pid && value) {
        let currentProduct = null;
        for(let i = 0, len = products.length; i<len; i++){
            let product = products[i];
            if (product.gsid == pid) {
                currentProduct = product;
                break;
            }
        }
        if (currentProduct) {
            let history = currentProduct.history;
            if(history && history.length > 0 && value.length>0){

                for (let i = 0, leni = value.length; i < leni; i++) {
                    let valueItem = value[i];
                    let isExist = false;
                    for (let j = 0, lenj = history.length; j < lenj; j++) {
                        let historyItem = history[j];
                        if (historyItem.mid === valueItem.mid) {
                            historyItem = valueItem;
                            isExist = true;
                        }
                    }
                    if (isExist == false) {
                        history.push(valueItem);
                    }
                }
            }else{
                currentProduct.history = value;
            }
        }
    }
}

function updateProductPraiseCount(products, pid, value){
    if(products && pid){
        for(let i = 0, len = products.length; i<len; i++){
            let product = products[i];
            if (product.gsid == pid) {
                product.praisenum = (parseInt(product.praisenum) + value);
                break;
            }
        }
    }
}

function updateHistoryPraiseCount(products, pid, mid, value){
    if(products && pid && mid){
        for(let i = 0, len = products.length; i<len; i++){
            let product = products[i];
            if (product.gsid == pid) {
                let history = product.history;
                if (history && history.length) {
                    for(let j = 0, len1 = history.length; j<len1; j++){
                        let historyItem = history[j];
                        if (historyItem.mid === mid) {
                            historyItem.praisenum = (parseInt(historyItem.praisenum) + value);
                        }
                    }
                }

                break;
            }
        }
    }
}

const initState = {
    product_status : 0, 
    message : '加载中', 
    results : []
};

export default function products(state = initState, action = {}) {
    const {
        type, payload
    } = action;

    switch (type) {
        //============================================================
        //获取产品列表
        //============================================================
        case types.FETCH_PRODUCTS:
            return { ...payload };

        case types.FETCH_PRODUCTS_SUCCESS:
            let successData = {
                product_status : 1,
                message : "获取策略列表成功",
                results : payload.results
            }
            return successData;
        case types.FETCH_PRODUCTS_FAILURE:
            let failureData = {
                product_status : -1,
                message : "获取策略列表失败",
                results : state.results
            }
            return failureData;

        //============================================================
        //产品关注
        //============================================================
        case types.SIGN_PRODUCT:
            state.pid = payload.pid;
            return {...state};
        case types.SIGN_PRODUCT_SUCCESS:
            //更新策略列表
            updateProductAttr(state.results, state.pid, "status",  "1");
            let signSuccessInfo = {
                product_sign_status : 1,
                message : "策略关注成功"
            };
            return {
               ...state,
               ...signSuccessInfo
            };
        case types.SIGN_PRODUCT_FAILURE:
            let signFailureInfo = {
                product_sign_status : -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...signFailureInfo
            };
        //============================================================
        //产品取消关注
        //============================================================
        case types.UNSIGN_PRODUCT:
            state.pid = payload.pid;
            return { ...state };

        case types.UNSIGN_PRODUCT_SUCCESS:
            //更新策略列表
            updateProductAttr(state.results, state.pid, "status",  "0");
            let unsignSuccessInfo = {
                product_unsign_status : 1,
                message : "取消关注成功"
            };
            return {
               ...state,
               ...unsignSuccessInfo
            };
        case types.UNSIGN_PRODUCT_FAILURE:
            let unsignFailureInfo = {
                product_unsign_status : -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...unsignFailureInfo
            };
        //============================================================
        //产品点赞
        //============================================================
        case types.PRAISE_PRODUCT:
            state.pid = payload.pid;
            state.type = payload.type;
            return { ...state };

        case types.PRAISE_PRODUCT_SUCCESS:

            //更新策略列表
            let praiseNum = payload.message?parseInt(payload.message):1;
            let praiseSuccessInfo = {
                product_praise_status : 1,
                message : "策略点赞成功"
            };

            updateProductPraiseCount(state.results, state.pid, praiseNum);
            
            return {
               ...state,
               ...praiseSuccessInfo
            };
        case types.PRAISE_PRODUCT_FAILURE:
            let praiseFailureInfo = {
                product_praise_status : -1,
                message: payload.message || '网络连接失败'
            };
            return {
               ...state,
               ...praiseFailureInfo
            };
        //============================================================
        //操作点赞
        //============================================================
        case types.PRAISE_HISTORY:
            state.pid = payload.pid;
            state.mid = payload.mid;
            state.history_praise_status = payload.history_praise_status;
            return { ...state };

        case types.PRAISE_HISTORY_SUCCESS:

            let historyPraiseNum = payload.message?parseInt(payload.message):1;

            updateHistoryPraiseCount(state.results, state.pid, state.mid, historyPraiseNum);
            let praiseHistorySuccessInfo = {
                history_praise_status : 1,
                message : "操作点赞成功"
            };
            return {
               ...state,
               ...praiseHistorySuccessInfo
            };
        case types.PRAISE_HISTORY_FAILURE:
            let praiseHistoryFailureInfo = {
                history_praise_status : -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...praiseHistoryFailureInfo
            };
        //============================================================
        //获取产品操作历史
        //============================================================
        case types.FETCH_HISTORYS:
            return { ...state, ...payload };

        case types.FETCH_HISTORYS_SUCCESS:
            updateProductHistory(state.results, state.pid, payload.results);
            let historySuccessInfo = {
                history_status : 1,
                message : "获取操作历史成功"
            };
            return {
               ...state,
               ...historySuccessInfo
            };
        case types.FETCH_HISTORYS_FAILURE:
            let historyFailureInfo = {
                history_status : -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...historyFailureInfo
            };
        //============================================================
        //获取产品当前持仓
        //============================================================
        case types.FETCH_HOLDINGS:
            state.pid = payload.pid;
            return { ...state };

        case types.FETCH_HOLDINGS_SUCCESS:
            updateProductAttr(state.results, state.pid, "holding", payload.results);
            let holdingSuccessInfo = {
                holding_status : 1,
                message : "获取当前持仓成功"
            };
            return {
               ...state,
               ...holdingSuccessInfo
            };
        case types.FETCH_HOLDINGS_FAILURE:
            let holdingFailureInfo = {
                holding_status : -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...holdingFailureInfo
            };
        //============================================================
        //产品复位
        //============================================================    
        case types.RESET_PRODUCTS:
            return { ...state, ...payload };
        default:
            return state;
    }
}